﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MsTestUtilities;

namespace MsTestUtilitiesTests.ExAssertTests
{
    [TestClass]
    public class ThrowsMessageTests
    {
        private const string ExpectedMessage = "My message";

        [TestMethod]
        public void ExpectedExceptionWithExpectedMessageThrown_PassAssertion()
        {
            var exception = GetException(() => { throw new InvalidOperationException(ExpectedMessage); });
            Assert.IsNull(exception);
        }

        [TestMethod]        
        public void NoExceptionThrown_FailAssertion()
        {
            var exception = GetException(() => { });
            Assert.IsNotNull(exception, "Assertion did not fail");
        }

        [TestMethod]
        public void ExpectedExceptionThrownWithoutMessage_FailAssertion()
        {
            var exception = GetException(() => { throw new InvalidOperationException(); });
            Assert.IsNotNull(exception, "Assertion did not fail");
        }

        [TestMethod]
        public void WrongExceptionThrown_FailAssertion()
        {
            var exception = GetException(() => { throw new IndexOutOfRangeException(); });
            Assert.IsNotNull(exception, "Assertion did not fail");
        }

        [TestMethod]
        public void ParentExceptionThrown_FailAssertion()
        {
            var exception = GetException(() => { throw new Exception(); });
            Assert.IsNotNull(exception, "Assertion did not fail");
        }

        private AssertFailedException GetException(Action action)
        {
            try
            {
                ExAssert.Throws<InvalidOperationException>(action, ExpectedMessage);
                return null;
            }
            catch(AssertFailedException ex)
            {
                return ex;
            }
        }
    }
}
